package thesis.database;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;

public class UserJDBCTemplate implements UserDAO {

	private DataSource dataSource;
	private JdbcTemplate jdbcTemplateObject;

	@Override
	public int create(User user) {
		String sql = "insert into User (user_name"
				+ ",password,user_type,name,birth_day,birth_place,gender,"
				+ "identify_number,address,phone_number,email,creation_time,last_update,"
				+ "last_login_time,is_active) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

		System.out.println("Created User Record");

		String md5Pass = generateMD5(user.getPassword());

		return jdbcTemplateObject.update(sql, user.getUserName(), md5Pass,
				user.getUserType(), user.getName(), user.getBirthDay(),
				user.getBirthPlace(), user.getGender(),
				user.getIdentifyNumber(), user.getAddress(),
				user.getPhoneNumber(), user.getEmail(), user.getCreationTime(),
				user.getLastUpdate(), user.getLastLoginTime(), user.isActive());
	}

	@Override
	public void setDataSource(DataSource ds) {
		this.dataSource = ds;
		this.jdbcTemplateObject = new JdbcTemplate(dataSource);

	}

	@Override
	public User getUser(Integer id) {
		String sql = "select * from user where user_id = ?";
		User user = jdbcTemplateObject.queryForObject(sql, new Object[] { id },
				new UserMapper());
		return user;
	}

	@Override
	public List<User> listUsers() {
		String sql = "select * from user";
		List<User> list = jdbcTemplateObject.query(sql, new UserMapper());
		return list;
	}

	@Override
	public int delete(Integer id) {
		String sql = "delete from user where user_id = ?";
		System.out.println("Deleted User Record with ID = " + id);
		return jdbcTemplateObject.update(sql, id);
	}

	@Override
	public int update(User user) {
		String sql = "update user set user_name = ?,password = ? ,user_type = ?,name = ?,birth_day = ?,"
				+ "birth_place = ? ,gender = ? ,"
				+ "identify_number= ? ,address = ?,phone_number = ? ,email = ? ,creation_time = ?"
				+ ",last_update = ? ,last_login_time = ? ,is_active = ? where user_id = ?";
		System.out.println("Updated User Record with ID = " + user.getUserId());

		return jdbcTemplateObject.update(sql, user.getUserName(),
				user.getPassword(), user.getUserType(), user.getName(),
				user.getBirthDay(), user.getBirthPlace(), user.getGender(),
				user.getIdentifyNumber(), user.getAddress(),
				user.getPhoneNumber(), user.getEmail(), user.getCreationTime(),
				user.getLastUpdate(), user.getLastLoginTime(), user.isActive(),
				user.getUserId());
	}

	@Override
	public User getUserByUsername(String userName) {
		String sql = "select * from user where user_name = ?";
		User user;
		try {
			user = jdbcTemplateObject.queryForObject(sql,
					new Object[] { userName }, new UserMapper());
		} catch (DataAccessException dae) {
			System.out.println("Doesn't exist user with username = " + userName);
			return null;
		}
		return user;
	}

	@Override
	public List<User> getListUserByName(String name) {
		List<User> users = null;
		String sql = "select * from user where name like ?";
		try {
			users = jdbcTemplateObject.query(sql, new UserMapper(), "%" + name
					+ "%");
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return users;
	}

	private String generateMD5(String str) {
		try {
			MessageDigest m = MessageDigest.getInstance("MD5");
			m.update(str.getBytes(), 0, str.length());
			return new BigInteger(1, m.digest()).toString(16);
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

}
